/*
 * The manager for the system tray icon of the application 
*/

package com.costeaalex.blueserver.systemtray;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import com.costeaalex.blueserver.threads.MainThread;


public class SystemTrayManager
	{

	private Image image = null;
	private SystemTray tray=null;
	private PopupMenu popup;
	private MenuItem defaultItem;
	private MainThread mTL;
	private PopupListener pL;
	private TrayIcon trayIcon;
	
	public SystemTrayManager()
		{
		}
	
	public SystemTrayManager(MainThread mT)
		{
		mTL=mT;
		}
	
	public void register()
		{
		if(SystemTray.isSupported())
			{
            
			tray = SystemTray.getSystemTray();
			image = Toolkit.getDefaultToolkit().getImage(getImageUrl("images/BlueServerIcon.jpg"));
			
			popup = new PopupMenu();
			pL=new PopupListener();
			
			if(mTL!=null)
				{
				defaultItem = new MenuItem("Connect");
				defaultItem.addActionListener(pL);
				if(mTL.isConnected())
					defaultItem.setEnabled(false);
				popup.add(defaultItem);
								
				defaultItem = new MenuItem("Disconnect");
				defaultItem.addActionListener(pL);
				if(!mTL.isConnected())
					defaultItem.setEnabled(false);
				popup.add(defaultItem);
				
				defaultItem = new MenuItem("Show Server");
				defaultItem.addActionListener(pL);
			    popup.add(defaultItem);
				}
			
		    defaultItem = new MenuItem("Exit");
		    defaultItem.addActionListener(pL);
		    popup.add(defaultItem);
		    
		    trayIcon = new TrayIcon(image, "Blue Server", popup);
		    trayIcon.setImageAutoSize(true);

		    try 
		    	{
		        tray.add(trayIcon);
		    	} 
		    catch (AWTException e) 
		    	{
		        System.err.println("TrayIcon could not be added.");
		    	}
			}
		}
	
	public boolean trayAvailable()
		{
		return SystemTray.isSupported();
		}
	
	private static URL getImageUrl(String fileName)
		{
	    URL url = ClassLoader.getSystemResource(fileName);
	    if (url == null)
		    {
	        try
		        {
		        url = new URL("file:" + fileName);
		        }
	        catch (Exception e)
		        {
		        e.printStackTrace();
		        }
		    }
	    return url;
		}
	
	public void disableMenuItem(String label)
		{
		for(int i=0; i<popup.getItemCount(); i++)
			if(popup.getItem(i).getLabel().equals(label))
				popup.getItem(i).setEnabled(false);
		}
	
	public void enableMenuItem(String label)
		{
		for(int i=0; i<popup.getItemCount(); i++)
			if(popup.getItem(i).getLabel().equals(label))
				popup.getItem(i).setEnabled(true);
		}
	
	public class PopupListener implements ActionListener
		{
		private MenuItem mI;
		
		public void actionPerformed(ActionEvent aE)
			{
			mI=(MenuItem) aE.getSource();

			if(mI.getLabel().equals("Exit"))
				mTL.destroy();
			
			if(mI.getLabel().equals("Connect"))
				mTL.connectTray();
			
			if(mI.getLabel().equals("Disconnect"))
				mTL.disconnectTray();
			
			if(mI.getLabel().equals("Show Server"))
				mTL.showServerTray();
			}
	
		}

	public void destroy()
		{
		tray.remove(trayIcon);
		}
	
	}
